<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .step-number{
            display: flex;
            font-size: 14px;
        }

        .step-number .step-reduce,
        .step-number .step-add{
            width: 40px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: grey;
        }

        .step-number .step-num{
            height: 40px;
            width: 120px;
            box-sizing: border-box;
            outline: none;
            /* text-indent: 10px; */
            text-align: center;
            border-radius: 0;
            border: 2px solid #333;
        }
        .disabled{
            cursor: not-allowed;
        }

        .step-1{
            margin: 20px;
        }
       
    </style>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        
        <p>
            数量: {{num}}
        </p>
        <p>
            <button @click="num--">num--</button>
            <button @click="num++">num++</button>
        </p>

        <hr>
        <!-- 父组件给子组件传数据和改变数据的方法(给子组件绑定自定义事件,事件触发时执行传入的函数) -->
        <!-- <laowang-step-number class="step-1" :count="num" v-on:aaaa="updateNum"></laowang-step-number> -->
        <!-- <laowang-step-number class="step-1" :count="num" @aaaa="updateNum"></laowang-step-number> -->


        <!-- <laowang-step-number class="step-1" :count="num" @update:count="v=>num=v"></laowang-step-number> -->


        <!-- Vue2存在 .sync修饰符 可以理解为是一个语法糖, 只要给组件的某个属性添加.sync修饰符 vue就会自动给该组件绑定自定义事件(update:xxx) 更新数据    -->
        <laowang-step-number class="step-1" :count.sync="num" ></laowang-step-number>


        <!-- Vue3 没有.sync修饰符  而是使用v-model代替 -->
        <!-- <laowang-step-number class="step-1" v-model:count="num" ></laowang-step-number> -->
    </div>
</body>
<script>

    /* 
        父组件传入的数据,子组件不能子组件不能直接修改,如果期望将子组件的数据同步到父组件,怎么办?


        组件通信:  子传父
            原理
            1. 父组件不仅传数据,还要传改变数据的方法(给子组件绑定自定义事件)
            2. 如果需要修改父组件传入的数据, 让子组件触发自定义事件,并传入更新后的新值 $emit("自定义事件名",data)

    */

    let LaowangStepNumber = {
        props:{
            count:{
                type:Number,
                default:1,
            }
        },
        template:`<div class="step-number">
            <div class="step-reduce" :class="{disabled: count <= 1}" @click="reduceCount">-</div>
            <input class="step-num" type="text" :value="count">
            <div class="step-add" @click="addCount">+</div>
        </div>`,
        methods:{
            reduceCount(){
                if(this.count <= 1) return false;
                this.$emit("update:count",this.count - 1);
            },
            addCount(){
                this.$emit("update:count",this.count + 1);
            }
        },
        mounted () {
            console.log("子组件Vue实例",this);
        }

    }
  


    var vm = new Vue({
        el: "#app",
        data: {
            num:1,
        },
        methods:{
            updateNum:function(val){  // 根组件挂载的函数 (this=>根组件)
                this.num = val;
            }
        },
        components: {
            LaowangStepNumber,
        }

    })





</script>

</html>